package com.clock.service.impl;


import com.clock.domain.entity.Role;
import com.clock.mapper.RoleMapper;
import com.clock.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2023-02-06 14:15:52
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<String> getRoleNameByRoleIdList(List<String> roleList) {
        List<String> roleNameList = new ArrayList<>();
        for (int i = 0; i < roleList.size(); i++) {
            String roleName = roleMapper.getRoleNameByRoleId(roleList.get(i));
            roleNameList.add(roleName);
        }
        return roleNameList;
    }

    @Override
    public String getRoleName(Integer roleId) {
        Role role = roleMapper.queryById(roleId);
        return role.getRoleName();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer roleId) {
        return this.roleMapper.queryById(roleId);
    }

    /**
     * 分页查询
     *
     * @param role 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Role> queryByPage(Role role, PageRequest pageRequest) {
        long total = this.roleMapper.count(role);
        return new PageImpl<>(this.roleMapper.queryAllByLimit(role, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(Role role) {
        this.roleMapper.insert(role);
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(Role role) {
        this.roleMapper.update(role);
        return this.queryById(role.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleId) {
        return this.roleMapper.deleteById(roleId) > 0;
    }
}
